<?php
########################################################
# DMS Donations Management System v1.0                 #
# created by:  Isaac Sabas                             #
#			   Nikko Reyes							   #
#			   Miko Tiamwatt						   #
#                                                      #
# De La Salle Philippines                              #
# All Rights Reserved DLSP Copyright 2009              #
########################################################

# INFORMATION:
# DBHandle.php 
# 	- this is the class that handles the connection of the application to the database
#	- this class handles the modification of system settings
# 	
# FUNCTION INFORMATION:
#	1. CONSTRUCTOR:
#		- sets up the database connection by loading the MDB.php library and the config.php file for the database connection credentials
#		- PEAR's MDB.php library is needed for this class to function
#		- the database connection credentials are located at /config.php
#	2. DB FUNCTIONS:
#		a. closeDBConnection () - disconnects the connection from database
#	3. SYSTEM MODIFICATION:
#		a. changeBirthdayReminderSetting (value) - change the birthday reminder interval
#		b. changePickupReminderSetting (value) - change the pickup reminder interval
#		c. changeFollowupReminderSetting (value) - change the birthday followup interval
#		d. changeCommisionRateSetting (value) - change the commision rate of the agents
#		e. changeAllowanceRateSetting (value) - change the allowance rate of the agents
#

class DBHandle{
# FOR CLASS MANAGEMENT #
# 	The following variables are set to uppercase. This means that they are   
#	class monitoring variables.		

	# FOR DETERMINING THAT THE CLASS HAS ALREADY BEEN INSTANTIATED #
	# boolean variable [ value is NULL when class has not been instantiated ]
		private $CLASS_INSTANTIATED = NULL;								             

	# CLASS MESSAGE_HANDLE HOLDER #
	# handles the error and notification messages of the class to notify the user 
	# to capture all SQL and system error, notifications and logs 
		public $MESSAGE_HANDLE = NULL;

	# DB_HANDLE INSTANCE HOLDER #
	# database connection handler [ MYSQL ]
	# instance of the database handle is placed here for the system to use the database with ease 
		public $DB_HANDLE = NULL;

# CLASS CONSTANT VARIABLES #
# this is for class management purposes
	private $errorArray = array("DB_NOTCONNECTED" => "Not connected to database.",
							    "DB_QUERYERROR" => "Problem with query",
							    "DB_INVALIDFIELD" => "Invalid field being accessed."
							   );

##############################
# SYSTEM'S CLASS CONSTRUCTOR #
##############################
	public function DBHandle(){	
		# this is to allow you to access the config.php values for the database	
			define ('ACCESS', true);
			$abs_root = '../';
		# include the needed / required libraries
			//require_once($abs_root . 'lib/PEAR/PEAR/MDB.php');
			//require_once($abs_root . 'config.php');
				$dbhost = 'localhost';
				$dbuser = 'root';
				$dbpass = '';
				$dbname = 'dms';
		# Data Source Name: This is the universal connection string
			//$dsn = "mysql://$dbuser:$dbpass@$dbhost/$dbname";

		# MDB::connect will return a PEAR MDB object on success or an PEAR MDB Error object on error
			//$this->DB_HANDLE = MDB::connect($dsn);
			if(!$this->DB_HANDLE = mysql_connect($dbhost, $dbuser, $dbpass)){
				die('Cannot connect to MySQL Database Server'); // remover when site to be deployed
			}
	
			if(!mysql_select_db($dbname, $this->DB_HANDLE)){
				die('Unable to select \'' . $dbname . '\' database'); // remove when site is to be deployed
			}			
	}	
		
	public function closeDBConnection () {
		mysql_close($this->DB_HANDLE);
	}
################################	
# MODIFY SYSTEM DATA FUNCTIONS #	
################################
	public function changeBirthdayReminderSetting ($value) {
		# check if the DB_HANDLE has connected to the database
			if ($this->DB_HANDLE == NULL) {	
				return $errorArray['DB_NOTCONNECTED'];
			}
			else {		
				$value = addslashes($value);
				# prepare the statement
					$statement = "UPDATE system_settings SET birthday_reminder_notification = '$value'";
				# query & check if error has occured
					if(!$query = mysql_query($statement)) {
						return $errorArray['DB_QUERYERROR'];
					}
					else {
						# debugging purposes <remove on production>	
							//echo "updated the database";
				
						# return the values											
							return true;
					}
			}
	}
	
	public function changePickupReminderSetting ($value) {
		# check if the DB_HANDLE has connected to the database
			if ($this->DB_HANDLE == NULL) {
				return $errorArray['DB_NOTCONNECTED'];
			}
			else {		
				$value = addslashes($value);
				# prepare the statement
					$statement = "UPDATE system_settings SET pickup_reminder_notification = '$value'";
				# query & check if error has occured
					if(!$query = mysql_query($statement)) {
						return $errorArray['DB_QUERYERROR'];
					}
					else {
						# debugging purposes <remove on production>	
							//echo "updated the database";
				
						# return the values											
							return true;
					}
			}
	}
	
	public function changeFollowupReminderSetting ($value) {
		# check if the DB_HANDLE has connected to the database
			if ($this->DB_HANDLE == NULL) {
				return $errorArray['DB_NOTCONNECTED'];
			}
			else {		
				$value = addslashes($value);
				# prepare the statement
					$statement = "UPDATE system_settings SET followup_reminder_notification = '$value'";
				# query & check if error has occured
					if(!$query = mysql_query($statement)) {
						return $errorArray['DB_QUERYERROR'];
					}
					else {
						# debugging purposes <remove on production>	
							//echo "updated the database";
				
						# return the values											
							return true;
					}
			}
	}
	
	public function changeCommisionRateSetting ($value) {
		# check if the DB_HANDLE has connected to the database
			if ($this->DB_HANDLE == NULL) {
				return $errorArray['DB_NOTCONNECTED'];
			}
			else {		
				$value = addslashes($value);
				# prepare the statement
					$statement = "UPDATE system_settings SET commission_rate = '" . $value . "'";
				# query & check if error has occured
					if(!$query = mysql_query($statement)) {
						return $errorArray['DB_QUERYERROR'];
					}
					else {
						# debugging purposes <remove on production>	
							//echo "updated the database";
				
						# return the values											
							return true;
					}
			}
	}
	
	public function changeAllowanceRateSetting ($value) {
		# check if the DB_HANDLE has connected to the database
			if ($this->DB_HANDLE == NULL) {
				return $errorArray['DB_NOTCONNECTED'];
			}
			else {		
				$value = addslashes($value);
				# prepare the statement
					$statement = "UPDATE system_settings SET allowance_rate = '" . $value . "'";
				# query & check if error has occured
					if(!$query = mysql_query($statement)) {
						return $errorArray['DB_QUERYERROR'];
					}
					else {
						# debugging purposes <remove on production>	
							//echo "updated the database";
				
						# return the values											
							return true;
					}
			}
	}
		
}	
?>
